from flask import Flask,jsonify,request
from utils import db

# 获取用户信息
def input_user_info():
    '''
    处理请求中的数据，并返回一个dict对象
    :return:处理的结果
    '''
    try:
        constent_type = request.headers['Content-Type']
        if constent_type == 'application/x-www-form-urlencoded':

            # 获取表单格式的请求参数
            # 用户信息获取
            remark = request.form.get('remark')
            telephone1 = request.form.get('telephone1')
            telephone2 = request.form.get('telephone2')
            account = request.form.get('account')
            password = request.form.get('password')

        elif constent_type == 'application/json':

            # 获取json格式的请求参数
            remark = request.json.get('remark')
            telephone1 = request.json.get('telephone1')
            telephone2 = request.json.get('telephone2')
            account = request.json.get('account')
            password = request.json.get('password')
        else:
            return {'code': 400, 'msg': '请求参数格式错误'}
        return {'remark':remark,'telephone1':telephone1,'telephone2':telephone2,'account':account,'password':password}

    except Exception as e:
        return {'error':str(e)}

# 获取账单信息
def input_bill_info():
    '''
    处理请求中的数据，并返回一个dict对象
    :return:处理的结果
    '''
    try:
        constent_type = request.headers.get('Content-Type')
        if constent_type == 'application/x-www-form-urlencoded':

            # 获取表单格式的请求参数
            # 用户信息获取
            # bill信息获取
            remark = request.form.get("remark")
            debt_date = request.form.get('debt_date')
            goods = request.form.get('goods')
            cargo_quantity = request.form.get('cargo_quantity')
            price = request.form.get('price')
            total_arrears = request.form.get('total_arrears')
            bill_status = request.form.get('bill_status')
            payment_date = request.form.get('payment_date')

        elif constent_type == 'application/json':

            # 获取json格式的请求参数
            remark = request.json.get("remark")
            debt_date = request.json.get('debt_date')
            goods = request.json.get('goods')
            cargo_quantity = request.json.get('cargo_quantity')
            price = request.json.get('price')
            total_arrears = request.json.get('total_arrears')
            bill_status = request.json.get('bill_status')
            payment_date = request.json.get('payment_date')

        else:
            return jsonify({'code': 400, 'msg': '请求参数格式错误'})
        
        return {'remark':remark, 'debt_date':debt_date,'goods':goods,'cargo_quantity':cargo_quantity,'price':price,'total_arrears':total_arrears,'bill_status':bill_status,'payment_date':payment_date}
    except Exception as e:
        return {'error':str(e)}

# 判断用户输入数据的规范性     
def norm_user_info():
    '''
    判断输入数据的规范性
    '''
    user = input_user_info()
    remark_sql = "SELECT remark FROM user WHERE remark = %s"
    account_sql = "SELECT account FROM user WHERE account = %s"

    # 检查必填字段是否为空
    if not user['remark'] or not user['account'] or not user['password']:
        return {'success': False, 'msg': '用户名、账号和密码不能为空'}

    # 检查用户名是否已存在
    if db.get_one(remark_sql, user['remark']) is not None:
        return {'success': False, 'msg': '用户名已存在'}

    # 检查账号是否已存在
    if db.get_one(account_sql, user['account']) is not None:
        return {'success': False, 'msg': '账号已存在'}

    # 检查至少一个电话号码存在
    if not user['telephone1'] and not user['telephone2']:
        return {'success': False, 'msg': '至少需要提供一个电话号码'}

    return {'success': True}

# 判断账单输入数据的规范性
def norm_bill_info():
    """
    判断输入数据的规范性
    """
    bill = input_bill_info()
    data = db.get_all("SELECT remark FROM user")
    remark_list = [user['remark'] for user in data]
    if bill['remark'] not in remark_list:
        return {'success':False,'msg':'用户名不存在'}
    elif bill['remark']==None or bill['debt_date'] == None or bill['goods'] == None or bill['cargo_quantity'] == None or bill['price'] == None or bill['total_arrears'] == None:
        return {'success':False,'msg':'输入信息不完整'}
    elif bill['payment_date'] == None and bill['bill_status'] == 1:
        return {'success':False,'msg':'已付款账单必须填写付款日期'}
    else:
        return {'success':True}
    


